<?php
// +-------------------------------------------------------------------
// |
// +-------------------------------------------------------------------
// | Copyright (c) 2009-2016 All rights reserved.
// +-------------------------------------------------------------------
namespace Service\Api\V1;

use Think\Exception;

class UploadS
{
    public $login = false; //是否需要登录才能访问该接口
    public $title = "上传多张图片ok";
    public $group = '公用';
    public $desc = "";

    public $input = [
//        'kcs_uploader_file' => 'required;file;label=图片文件;',
    ];

    public $output = [
        'url' => [
            [
                'url' => 'label=图片路径;'
            ]
        ],
    ];

    public function run($param, $uid)
    {

//        $param = $this->request;
//       var_dump($_FILES);
        $file = $_FILES['file'];

        if(empty($file))  throw new \Exception('请选择要上传的文件');
        unset($_FILES['file']);
        $count = count($file['name']);       // 上传图片的数量
        if($count > 9) throw new \Exception('批量上传图片一次最多上传9张图片');
        $tmpFile  = [];
        $returnData = [];
        for($i=0;$i<$count;$i++)          // 循环处理图片
        {
            $tmpFile['name']   = $file['name'][$i];
            $tmpFile['type']   = $file['type'][$i];
            $tmpFile['tmp_name'] = $file['tmp_name'][$i];
            $tmpFile['error']  = $file['error'][$i];
            $tmpFile['size']   = $file['size'][$i];
            $_FILES['file_'.$i] = $tmpFile;
            // 判断是否是允许的图片类型
            $ext = substr($_FILES['file_'.$i]['name'],strrpos($_FILES['file_'.$i]['name'],'.')+1); // 上传文件后缀
//            stripos('jpeg|png|bmp|jpg',$ext) === FALSE && $this->ajaxReturn(['code' => -1, 'message' => '图片格式支持 JPEG、PNG、BMP格式图片'], 'JSON');
            $data = $this->uploadOne('file_'.$i,'jpeg|png|bmp|jpg');
            if($data['status'] == 1)
            {
                throw new \Exception('第'.($i+1).'张图片上传失败，'.$data['msg']);
            }
            $returnData[$i]['url']   = $data['url'];   // 图片路径
            $returnData[$i]['old_name'] = substr($tmpFile['name'],0,strrpos($tmpFile['name'], '.')); // 图片原名称
        }
        return [
            'url' => $returnData
        ];


    }

    private function uploadOne($filename = 'file',$type = 'jpeg|png|bmp|jpg',$maxSize = 10240)
    {
        list($width,$height)    = getimagesize($_FILES[$filename]['tmp_name']); // 获取图片的宽和高
        list($usec, $sec) = explode(" ", microtime());
        $time = $sec.substr($usec,2);                         // 秒数+微秒数
        $ext = substr($_FILES[$filename]['name'],strrpos($_FILES[$filename]['name'],'.')+1); // 上传文件后缀
        $name   = $time.'-'.$width.'*'.$height.'.'.$ext;
        $filePath = $_FILES[$filename]['tmp_name'];
        $type   = $_FILES[$filename]['type'];

//        $returnData['url'] = $this->qiniu->upload($name,$filePath,$type);
        copy($filePath, ROOT_PATH . '/Uploads/Picture/' . $name);
        $returnData['url'] = '/Uploads/Picture/' . $name;
        $returnData['status'] = 0;
        return $returnData;
    }
}
